﻿using oueb;
using System.Collections.Concurrent;
using System.Text;

namespace aoorec
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            var oserve = new OuebServer("http://127.0.0.2:10023");
            oserve.UseCors(op => op.AllowAnyHeaders().AllowAnyMethod().AllowAnyOrigin());

            var list = new ConcurrentQueue<string>();
            var list2 = new ConcurrentQueue<string>();
            list.Enqueue("123");
            list.Enqueue("123");
            list.Enqueue("123");
            list.Enqueue("123");
            list.Enqueue("1233");
            oserve.AddRoute(null, context =>
            {
                if (context.Request.Method == "OPTIONS")
                {
                    context.Response.StatusCode = oueb.Ceen.HttpStatusCode.NoContent;
                    context.Response.AddHeader("Allow", "GET, POST, OPTIONS");
                    return Task.FromResult(true);
                }
                return Task.FromResult(false);
            });
            oserve.AddRoute("/list", async context =>
            {
                var text = new StringBuilder();
                while (list.TryDequeue(out var text1)) text.AppendLine(text1);
                var __text = text.ToString();
                if (!string.IsNullOrEmpty(__text)) list2.Enqueue(__text);
                await context.Response.WriteAllJsonAsync(text.ToString());
                return true;
            });
            oserve.AddRoute("/all", async context =>
            {
                var text = new StringBuilder();
                
                var __text = text.ToString();
                if (!string.IsNullOrEmpty(__text)) list2.Enqueue(__text);
                await context.Response.WriteAllJsonAsync(text.ToString());
                return true;
            });
            oserve.AddRoute(null, async context =>
            {
                await context.Response.WriteAllJsonAsync("test");
                return true;
            });
            oserve.Listen();
            lock (oserve) while (oserve.Runing) Monitor.Wait(oserve, 1500);
        }
    }
}
